package wordCount

/**
  * 单机版的wordCount
  */

object WorldCount {

  def main(args: Array[String]): Unit = {
    //初始数据
    val words = List("hello tom hello jerry","hello kitty hello hello")

    /*val arrays = words.map(_.split(" "))   //形成List<Array<String>>的列表

    val flatten = arrays.flatten           //形成List<String>
    for(i <- flatten)
      println(i)*/

    val flatten = words.flatMap(_.split(" "))      //形成List<String>
    //for(i <- flatten)
      //println(i)

    val wordsAndOne = flatten.map((_,1))    //形成List<元组>
    //println(wordsAndOne)    List((hello,1), (tom,1), (hello,1), (jerry,1), (hello,1), (kitty,1), (hello,1), (hello,1))
        //(hello,1)可以看成一个元组，
        //val yuanzu = (hello,1)    取hello用 yuanzu._1 获取
    val groupd = wordsAndOne.groupBy(_._1)
    println(groupd)   //Map[String,List[(String,1)]]
    //val result = groupd.map(_._1, _._2.size)         //这样写出错
    val result = groupd.map(t => (t._1,t._2.size))
    println(result)
    val finalResult = result.toList.sortBy(_._2)
    println(finalResult)
    val finalResult2 = result.toList.sortBy(_._2).reverse
    println(finalResult2)

    val finalWords = words.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).map(t => (t._1,t._2.size)).toList.sortBy(_._2)
    println(finalWords)
  }
}




